package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TRank;
import com.cookor.poi.modules.phy.service.TRankService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tRank"})
public class TRankController extends BaseController {
  @Autowired
  private TRankService tRankService;
  
  @ModelAttribute
  public TRank get(@RequestParam(required = false) String id) {
    TRank entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tRankService.get(id); 
    if (entity == null)
      entity = new TRank(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tRank:view"})
  @RequestMapping({"list", ""})
  public String list(TRank tRank, HttpServletRequest request, HttpServletResponse response, Model model) {
    tRank.setTenantId(UserUtils.getTenantId());
    Page<TRank> page = this.tRankService.findPage(new Page(request, response), tRank);
    model.addAttribute("tRank", tRank);
    model.addAttribute("page", page);
    return "modules/phy/tRankList";
  }
  
  @RequiresPermissions({"phy:tRank:view"})
  @RequestMapping({"form"})
  public String form(TRank tRank, Model model) {
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tRank", tRank);
    return "modules/phy/tRankForm";
  }
  
  @RequiresPermissions({"phy:tRank:edit"})
  @RequestMapping({"save"})
  public String save(TRank tRank, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tRank, new Class[0]))
      return form(tRank, model); 
    this.tRankService.save(tRank);
    addMessage(redirectAttributes, new String[] { "保存职称成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tRank/?repage";
  }
  
  @RequiresPermissions({"phy:tRank:edit"})
  @RequestMapping({"delete"})
  public String delete(TRank tRank, RedirectAttributes redirectAttributes) {
    this.tRankService.delete(tRank);
    addMessage(redirectAttributes, new String[] { "删除职称成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tRank/?repage";
  }
  
  @RequiresPermissions({"user"})
  @ResponseBody
  @RequestMapping({"treeData"})
  public List<Map<String, Object>> treeData(@RequestParam(required = false) String rankName, HttpServletResponse response) {
    List<Map<String, Object>> mapList = Lists.newArrayList();
    TRank tRank = new TRank();
    tRank.setRankName(rankName);
    tRank.setTenantId(UserUtils.getTenantId());
    List<TRank> list = this.tRankService.findList(tRank);
    for (int i = 0; i < list.size(); i++) {
      TRank e = list.get(i);
      Map<String, Object> map = Maps.newHashMap();
      map.put("id", e.getId());
      map.put("name", e.getRankName());
      mapList.add(map);
    } 
    return mapList;
  }
}
